/**Created by the LayaAirIDE*/
module view.dlg{
	export class Task extends ui.dlg.TaskUI{
		private static task : Task;

		private constructor(){
			super();
			this.init();
			this.addChild(Game.gainAward);
		}

		static getTask():Task{
			if(!Task.task) Task.task = new Task();
			return Task.task;
		}

		init():void{
			this.isModal=true;
			this.popupCenter=true;
			this.taskDailyList.repeatY = Cfg.TaskCfg["daily_count"]["value"];
			this.taskBg.on(Laya.Event.MOUSE_DOWN,this,this.noClose);
			this.taskCloseBtn.on(Laya.Event.MOUSE_DOWN,this,this.closeTask);
			this.taskDailyList.renderHandler = new Laya.Handler(this, this.dailyRender);
			this.taskTab.on(Laya.Event.CHANGE,this,this.changeTab);

			this.taskDaily.visible = true;
			this.taskAction.visible = false;
			this.taskTitleText.skin = "task/task_title_daily.png";
		}

		//打开任务窗口
		openTask():void{
			this.showDaily();
			this.showAction(0);
			this.popup();
		}

		//关闭任务
		closeTask():void{
			this.close();
		}
		noClose():void{}

		changeTab():void{
			if(this.taskTab.selectedIndex == 0){
				this.taskDaily.visible = true;
				this.taskAction.visible = false;
				this.taskTitleText.skin = "task/task_title_daily.png";
			}else if(this.taskTab.selectedIndex == 1){
				this.taskDaily.visible = false;
				this.taskAction.visible = true;
				this.taskTitleText.skin = "task/task_title_action.png";
			}
		}

		showDaily() : void{
			this.showDailyActive();
			this.taskDailyList.array = taskModel.getDailyOrder();
		}

		//渲染每日任务
		dailyRender(cell : Laya.Box, index : number) : void{
			let daily = this.taskDailyList.array[index];
			let cfgDaily=Cfg.TaskCfg["daily_"+daily["id"]];
			cell.getChildByName("taskDailyText")["text"] = cfgDaily["des"];
			cell.getChildByName("taskDailyIco")["skin"] = "task/task_daily_ico_"+cfgDaily["mode"]+".png";
			cell.getChildByName("taskDailyBtn").off(Laya.Event.CLICK,this,this.dailyHaving);
			cell.getChildByName("taskDailyBtn").off(Laya.Event.CLICK,this,this.taskFinish);
			switch(daily["state"]){
				case -2://已结束
					cell.getChildByName("taskDailyPro")["value"] = 1
					cell.getChildByName("taskDailyCount")["text"] = cfgDaily["count"]+"/"+cfgDaily["count"];
					cell.getChildByName("taskDailyBtn")["skin"] = "task/task_daily_btn_02.png";
					break;
				case -1://已完成
					cell.getChildByName("taskDailyPro")["value"] = 1
					cell.getChildByName("taskDailyCount")["text"] = cfgDaily["count"]+"/"+cfgDaily["count"];
					cell.getChildByName("taskDailyBtn")["skin"] = "task/task_daily_btn_01.png";
					cell.getChildByName("taskDailyBtn").on(Laya.Event.CLICK,this,this.taskFinish,[daily.id,Cfg.TaskCfg["type_daily"]["value"],0]);
					break;
				default://进行中
					cell.getChildByName("taskDailyPro")["value"] =  daily.state/cfgDaily["count"]
					cell.getChildByName("taskDailyCount")["text"] = daily.state+"/"+cfgDaily["count"];
					cell.getChildByName("taskDailyBtn")["skin"] = "task/task_daily_btn_1.png";
					cell.getChildByName("taskDailyBtn").on(Laya.Event.CLICK,this,this.dailyHaving,[daily.id]);
					break;
			}
			for(let j=0; j<cfgDaily["award"].length; j++){
				if(cfgDaily["award"][j] == Cfg.TaskCfg["award_prop"]["value"]){
					cell.getChildByName("taskDailyPrize"+j)["skin"] = "item/"+Cfg.ItemCfg[cfgDaily["supply"][j]]["skin"]+".png";
				} else{
					cell.getChildByName("taskDailyPrize"+j)["skin"] = "task/integral_ico_"+cfgDaily["award"][j]+".png";
				} 
				cell.getChildByName("taskDailyReward"+j)["text"] = "+"+cfgDaily["reward"][j];
			}
		}

		//显示每日任务活跃
		showDailyActive():void{
			let dailyActive = KBEngine.app.player().task_daily_active;
			this.taskActivePro.value = dailyActive / Cfg.TaskCfg["daily_active_full"]["value"] ;
			let activeGain = KBEngine.app.player().task_daily_active_gain.split(",");
			for(let i=0;i<activeGain.length;i++){
				let active = this.taskActive.getChildByName("taskActivePrize_"+i);
				active.off(Laya.Event.CLICK,this,this.taskDailyActiveReceive);
				active.on(Laya.Event.MOUSE_DOWN,this,this.showActiveTime,[i]);
				active.on(Laya.Event.MOUSE_UP,this,this.hideActiveDes,[i]);
				active.on(Laya.Event.MOUSE_OUT,this,this.hideActiveDes,[i]);
				if(dailyActive >= Cfg.TaskCfg["daily_active_"+i]["count"]){
					if(activeGain[i] == Cfg.TaskCfg["state_finish"]["value"]){
						active["skin"] = "task/active_gift_finish.png";
					}else{
						active["skin"] = "task/active_gift_bright.png";
						active.on(Laya.Event.CLICK,this,this.taskDailyActiveReceive,[i]);
					}
				}else{
					active["skin"] = "task/active_gift_dark.png";
				}
			}
		}
		
		//显示活动任务
		showAction(index:number):void{
			let actionList = KBEngine.app.player().task_action_list;
			let action = Cfg.TaskCfg["action_"+actionList[index]["id"]];
			let state = actionList[index]["state"].split(",");
			//左右按钮显示问题
			this.taskAction.getChildByName("arrowLeft")["visible"] = true;
			this.taskAction.getChildByName("arrowRight")["visible"] = true;
			if(index <= 0){
				index = 0;
				this.taskAction.getChildByName("arrowLeft")["visible"] = false;
			}
			if(index >= actionList.length-1){
				index = actionList.length-1;
				this.taskAction.getChildByName("arrowRight")["visible"] = false;
			}
			this.taskAction.getChildByName("arrowLeft").offAll()
			this.taskAction.getChildByName("arrowLeft").on(Laya.Event.CLICK,this,this.showAction,[index-1]);
			this.taskAction.getChildByName("arrowRight").offAll()
			this.taskAction.getChildByName("arrowRight").on(Laya.Event.CLICK,this,this.showAction,[index+1]);
			//根据不同活动类型进行不同渲染
			this[Cfg.TaskCfg[action["way"]]["value"]](action, state);
		}

		//渲染累计活动任务
		action_way_pile(action, state):void{
			let actionBox : Laya.Box = this.taskAction.getChildByName("actionPile") as Laya.Box;
			actionBox.getChildByName("actionBtn").off(Laya.Event.CLICK,this,this.taskFinish);
			actionBox.getChildByName("actionBtn").off(Laya.Event.CLICK,this,this.actionHaving);
			for(let i=0; i<state.length; i++){
				if(state[i] == -2){
					if(i == state.length-1){
						actionBox.getChildByName("actionBtn")["skin"] = "task/task_daily_btn_02.png";
						actionBox.getChildByName("actionProgress")["value"] = 1;
						actionBox.getChildByName("actionCount")["text"] = action["count"][i][0]+"/"+action["count"][i][0];
					}else{
						continue;
					}
				}else if(state[i] == -1){
					actionBox.getChildByName("actionBtn")["skin"] = "task/task_daily_btn_01.png";
					actionBox.getChildByName("actionBtn").on(Laya.Event.CLICK,this,this.taskFinish,[action["id"],Cfg.TaskCfg["type_action"]["value"],i]);
					actionBox.getChildByName("actionProgress")["value"] = 1;
					actionBox.getChildByName("actionCount")["text"] = action["count"][i][0]+"/"+action["count"][i][0];
				}else{
					actionBox.getChildByName("actionBtn")["skin"] = "task/task_daily_btn_1.png";
					actionBox.getChildByName("actionBtn").on(Laya.Event.CLICK,this,this.actionHaving,[action["id"],i]);
					actionBox.getChildByName("actionProgress")["value"] = state[i]/action["count"][i][0];
					actionBox.getChildByName("actionCount")["text"] = state[i]+"/"+action["count"][i][0];
				}
				actionBox.getChildByName("actionName")["text"] = action["name"];
				actionBox.getChildByName("actionDes")["text"] = action["des"];
				actionBox.getChildByName("actionImg")["skin"] = action["img"][i][0];
				let award = action["award"][i];
				let awardBox : Laya.Box = actionBox.getChildByName("taskActionPrize") as Laya.Box;
				awardBox.removeChildren();
				for(let j=0; j<award.length; j++){
					let taskAward :view.module.taskPrize = new view.module.taskPrize();
					if(award[j] == Cfg.TaskCfg["award_prop"]["value"]){
						taskAward.award.skin = "item/"+Cfg.ItemCfg[action["supply"][i][j]]["skin"]+".png";
					}else{
						taskAward.award.skin = "task/integral_ico_"+award[j]+".png";
					}
					taskAward.reward.text = "+"+action["reward"][i][j];
					taskAward.left = awardBox.width/(award.length + 1) * (j + 1) - taskAward.width/2;
					awardBox.addChild(taskAward);
				}
				break;
			}
		}
		//渲染兑换活动任务(预留)
		action_way_exchange(action, state):void{
		}

		showActiveTime(index:number,e:Event):void{
			console.log("showActiveTime");
			Laya.timer.once(500,this,this.showActiveDes,[index,e]);
		}
		//显示活跃奖励描叙
		showActiveDes(index:number,e):void{
			let award = Cfg.TaskCfg["daily_active_"+index];
			if(award["mode"] == Cfg.TaskCfg["award_mode_fixed"]["value"]){
				this.awardDes.getChildByName("award")["skin"] = "task/integral_ico_"+award["award"]+".png";
				this.awardDes.getChildByName("reward")["text"] = "+"+award["reward"];
			}else if(award["mode"] == Cfg.TaskCfg["award_mode_prop_fixed"]["value"]){
				this.awardDes.getChildByName("award")["skin"] = "item/"+Cfg.ItemCfg[award["reward"][0]]["skin"]+".png";
				this.awardDes.getChildByName("reward")["text"] = "+"+award["reward"][1];
			}else if(award["mode"] == Cfg.TaskCfg["award_mode_random"]["value"]){
				this.awardDes.getChildByName("award")["skin"] = "task/integral_ico_"+award["award"]+".png";
				this.awardDes.getChildByName("reward")["text"] = "+"+award["reward"][0]+"~"+award["reward"][1];
			}
			let point = this.taskActive.getChildByName("taskActivePrize_"+index)["localToGlobal"](new laya.maths.Point(0,0));
			this.awardDes.x = this.awardDes.parent["globalToLocal"](new laya.maths.Point(point.x, 0)).x;
			this.awardDes.visible = true ;
			this.taskActive.getChildByName("taskActivePrize_"+index).off(Laya.Event.CLICK,this,this.taskDailyActiveReceive);
		}
		//隐藏活跃奖励描叙
		hideActiveDes(index:number,e):void{
			Laya.timer.clear(this,this.showActiveDes);
			if(this.awardDes.visible){
				Laya.timer.once(500,this,this.showDailyActive,[index]);
				this.awardDes.visible = false ;
			}
		}

		//前往进行中的每日任务
		dailyHaving(id):void{
			let daily = Cfg.TaskCfg["daily_"+id];
			this["task_have_"+Cfg.TaskCfg[daily["mode"]]["value"]](daily["condition"],daily["extras"][0]);
		}
		//前往进行中的活动任务
		actionHaving(id:number, stage:number):void{
			let action = Cfg.TaskCfg["action_"+id];
			this["task_have_"+Cfg.TaskCfg[action["mode"]]["value"]](action["condition"][stage][0],action["extras"][stage][0]);
		}

		//完成任务,提交至服务器
		taskFinish(id:number,type:number, stage:number, e:Laya.Event){
			CtrlManager.GetCtrl(CtrlNames.Task).taskFinish(id,type,stage);
		}

		//完成任务,领取奖励
		onTaskFinish(id, type, stage):void{
			if(type == Cfg.TaskCfg["type_daily"]["value"]){
				let awards = Cfg.TaskCfg["daily_"+id]["award"];
				for(let i=0; i<awards.length; i++){
					this["task_gain_"+Cfg.TaskCfg[awards[i]]["value"]](Cfg.TaskCfg["daily_"+id]["reward"][i],
						Cfg.TaskCfg["daily_"+id]["supply"][i], new laya.maths.Point(360,640));
				}
				Laya.timer.once(500, this, this.showDaily);
			}else if(type == Cfg.TaskCfg["type_action"]["value"]){
				let awards = Cfg.TaskCfg["action_"+id]["award"][stage];
				for(let i=0; i<awards.length; i++){
					this["task_gain_"+Cfg.TaskCfg[awards[i]]["value"]](Cfg.TaskCfg["action_"+id]["reward"][stage][i],
						Cfg.TaskCfg["action_"+id]["supply"][stage][i], new laya.maths.Point(310,915));
				}
				
				this.showAction(ComFun.GetArrayValueIndex(KBEngine.app.player().task_action_list,"id",id));
			}
		}

		//领取活跃奖励
		taskDailyActiveReceive(index):void{
			CtrlManager.GetCtrl(CtrlNames.Task).taskDailyActiveReceive(index);
		}
		//完成活跃奖励领取
		onTaskDailyActiveReceive(index):void{
			this.showDailyActive();
			//播放动效
			let active = Cfg.TaskCfg["daily_active_"+index];
			let prize : laya.ui.Image = this.taskActive.getChildByName("taskActivePrize_"+index) as laya.ui.Image;
			let initPoint = new laya.maths.Point();
			initPoint = ComFun.pointChange(prize,this,initPoint);
			this["task_gain_"+Cfg.TaskCfg[active["award"]]["value"]](active["reward"][1], active["reward"][0], initPoint);
		}

		task_have_mode_plant(condition:number, extras:number):void{
			this.closeTask();
			if(condition == Cfg.TaskCfg["condition_quality"]["value"]){
				CtrlManager.GetCtrl(CtrlNames.Warehouse).ShowDlgWithRate(extras);
			}else{
				CtrlManager.GetCtrl(CtrlNames.Warehouse).ShowDlgWithRate(Cfg.rateLevel["ur"]);
			}
		}

		task_have_mode_worm():void{
			this.closeTask();
		}

		task_have_mode_harvest():void{
			this.closeTask();
		}

		task_have_mode_weed():void{
			this.closeTask();
		}

		task_have_mode_water():void{
			this.closeTask();
		}

		task_have_mode_dial():void{
			this.closeTask();
			Game.viewFarm.LuckDarwFun();
		}

		task_have_mode_friend():void{
			this.closeTask();
			console.log("前往好友农场");
		}

		task_have_mode_online():void{
			this.closeTask();
			console.log("显示在线时长")
		}

		task_have_mode_steal():void{
			this.closeTask();
			console.log("前往好友农场");
		}

		task_have_mode_prop():void{
			this.closeTask();
			CtrlManager.GetCtrl(CtrlNames.Warehouse).ShowDlgWithRate(Cfg.rateLevel["item"]);
		}

		task_have_mode_():void{
			this.closeTask();
		}

		task_have_mode_finish_task():void{

		}

		task_have_mode_ad():void{
			this.closeTask();
			console.log("打开广告");
		}

		task_have_mode_share():void{
			this.closeTask();
			CtrlManager.GetCtrl(CtrlNames.Share).shareCommon();
		}

		//奖励领取
		task_gain_award_active(reward, supply, initPoint):void{
			Game.gainAward.playActive();
			// let overPoint = new laya.maths.Point();
			// overPoint = ComFun.pointChange(this.taskActivePro,this,overPoint);
			// Game.gainAward.playAni(300, 500, overPoint.x, overPoint.y,this, "task/integral_ico_3000.png");
		}
		task_gain_award_integral(reward, supply, initPoint):void{
			let overPoint = new laya.maths.Point();
			overPoint = ComFun.pointChange(Game.viewFarm.imgScore,this,overPoint);
			Game.gainAward.playAni(initPoint.x, initPoint.y, overPoint.x, overPoint.y,this, "task/integral_ico_3001.png");
		}
		task_gain_award_(reward, supply, initPoint):void{
			console.log("领取护盾动效")
		}
		task_gain_award_prop(reward, supply, initPoint):void{
			let overPoint = new laya.maths.Point();
			overPoint = ComFun.pointChange(Game.viewFarm.btnWarehouse,this,overPoint);
			Game.gainAward.playAni(initPoint.x, initPoint.y, overPoint.x, overPoint.y,this, "item/"+Cfg.ItemCfg[supply]["skin"]+".png");
		}
		task_gain_award_turntable(reward, supply, initPoint):void{
			let overPoint = new laya.maths.Point();
			overPoint = ComFun.pointChange(Game.viewFarm.imgScore,this,overPoint);
			Game.gainAward.playAni(initPoint.x, initPoint.y, overPoint.x, overPoint.y,this, "item/"+Cfg.ItemCfg[supply]["skin"]+".png");
		}
	}
	
}
